package trees.tree

import trees.settings.Settings

object IdFactory {

    import _root_.scala.collection.mutable.HashMap



    private val intToLabel = new HashMap[Int,String]()
    private val labelToInt = new HashMap[String,Int]()
     
    
    private var curNr=0

    //Fulhack.... where do I initialize a singleton
    private var initialized=false

    private def initialize():Boolean = {
        for (key <- Settings.leafIds.keys){
            var id=Settings.leafIds.get(key).get
            curNr=if(id>curNr) {id} else {curNr}

            labelToInt.put(key, id)
            intToLabel.put(id,key)
        }
        true
    }

    def contains(label:String):Boolean = {
        labelToInt.contains(label)
    }

    def contains(id:Int):Boolean = {
        intToLabel.contains(id)
    }

    def getNoOfIDs():Int = {
        curNr
    }
    def getID(label:String):Int = {
        if(!initialized){
            initialized=initialize()
        }

        (labelToInt get label) match {
            case Some(id:Int) => return id
            case None =>
                //generate a new identifier
                curNr+=1
                intToLabel.put(curNr,label)
                labelToInt.put(label,curNr)
                return curNr
        }
    }
  
    def getLabel(id:Int):String = {
        if(!initialized){
            initialized=initialize()
        }
        (intToLabel get id) match {
            case Some(label:String) => return label
            case None =>
                //Error... throw error or return generic string id
                throw new Error("Unknown id: "+id)
        }
    }
}
